package com.binance.assetservice.web.configs;

import com.binance.master.configs.RestClientConfig;
import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.client.RestTemplate;

@Configuration
public class CustomRestTemplateConfig extends RestClientConfig {

    @Bean
    public CustomRestTemplate customRestTemplate(HttpClient httpClient) {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
        factory.setConnectTimeout(10 * 1000);
        factory.setReadTimeout(30 * 1000);
        return new CustomRestTemplate(new RestTemplate(factory));
    }

    public static class CustomRestTemplate {

        private final RestTemplate restTemplate;

        public CustomRestTemplate(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }

        public RestTemplate getRestTemplate() {
            return this.restTemplate;
        }
    }

}
